package com.company.project.web.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.enums.RestCodeEnum;
import com.company.project.common.utils.PasswordUtil;
import com.company.project.common.utils.RestUtil;
import com.company.project.common.pojo.Rest;
import com.company.project.common.utils.JwtUtil;
import com.company.project.web.system.pojo.user.UserFrom;
import com.company.project.web.system.pojo.user.User;
import com.company.project.web.system.pojo.user.UserInfoVO;
import com.company.project.web.system.pojo.user.UserSelectVO;
import com.company.project.web.system.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-08-09
 */
@Api(value = "用户",tags = "用户")
@Controller
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "登录")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "username", value = "用户名",  required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "password", value = "密码",  required = true, dataType = "String")
    })
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Rest<UserSelectVO> login(String username, String password) throws Exception {
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            return RestUtil.rest(RestCodeEnum.CODE_400.getCode(), "账号和密码不能为空");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginname", username);
        queryWrapper.eq("password", PasswordUtil.generatePassword(password));

        User user = userService.getOne(queryWrapper);

        if (user == null){
            return RestUtil.rest(RestCodeEnum.CODE_403.getCode(), "账号或密码不正确");
        }else {
            Map map = new HashMap();
            map.put("user_id", user.getId());
            map.put("username", user.getUsername());
            map.put("roleId", user.getRoleId());

            // 对象转字符串
            ObjectMapper objectMapper = new ObjectMapper();
            String subject = objectMapper.writeValueAsString(map);

            //24小时60分钟60秒1000毫秒
            String token = JwtUtil.createJWT("jwt唯一标识", "签发人", subject, 24*60*60*1000, map);
            map.put("xxxToken", token);

            return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc() , map);
        }
    }

    @ApiOperation(value = "退出")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
    })
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    @ResponseBody
    public Rest logout() {
        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc() );
    }

    @ApiOperation(value = "获取技术列表")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
    })
    @RequestMapping(value = "getTechnicianList", method = RequestMethod.GET)
    @ResponseBody
    public Rest getTechnicianList(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.eq("role_id", 2);

        List<User> userList = userService.list(queryWrapper);

        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc() , userList);
    }

    @ApiOperation(value = "获取登录用户")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
    })
    @RequestMapping(value = "getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public Rest<UserInfoVO> getUserInfo(){
        Claims claims = JwtUtil.parseJWT(request.getHeader("xxxToken"));
        Integer user_id = claims.get("user_id", Integer.class);

        User user = userService.getById(user_id);
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(user,userInfoVO);

        user.setImgUrl("http://f.hiphotos.baidu.com/image/h%3D300/sign=d985fb87d81b0ef473e89e5eedc551a1/b151f8198618367aa7f3cc7424738bd4b31ce525.jpg");
        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc(), userInfoVO);
    }

    @ApiOperation(value = "修改用户")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "xxxToken", value = "请求头token", required = true, dataType = "String"),
    })
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    @ResponseBody
    public Rest updateUser(UserFrom userFrom){
        User user = new User();
        BeanUtils.copyProperties(userFrom, user);

        userService.updateById(user);

        return RestUtil.rest(RestCodeEnum.CODE_200.getCode(), RestCodeEnum.CODE_200.getDesc());
    }

}

